package com.android.phoenix.activity.discovery;


import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.android.phoenix.R;
import com.android.phoenix.activity.BaseActivity;
import com.android.phoenix.bean.ActivityInfo;
import com.android.phoenix.util.DataBaseUtil;

public class BycecleActivity extends BaseActivity{
	
	private ImageView imgBack;
	private RadioGroup rg;
	private ListView listActivityInfo;
	private BaseAdapter mAdapter;
	private static SparseArray<ActivityInfo> allActivityMap = new SparseArray<ActivityInfo>();
	private static SparseArray<ActivityInfo> myActivityMap = new SparseArray<ActivityInfo>();
	
	private TextView tvReleaseActivity;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acti_bycecle);
		
		imgBack = (ImageView) findViewById(R.id.imgBack);
		imgBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		tvReleaseActivity = (TextView) findViewById(R.id.tvReleaseActivity);
		tvReleaseActivity.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(BycecleActivity.this, ReleaseActivity.class);
				startActivity(intent);
			}
		});
		
		listActivityInfo = (ListView) findViewById(R.id.listActivityInfo);
		mAdapter = new InfoListAdapter(allActivityMap);
		listActivityInfo.setAdapter(mAdapter);
		
		rg = (RadioGroup)findViewById(R.id.rg);
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch(checkedId) {
				case R.id.rbAllActivity:
					mAdapter = new InfoListAdapter(allActivityMap);
					listActivityInfo.setAdapter(mAdapter);
					mAdapter.notifyDataSetChanged();
					break;
				case R.id.rbMyActivity:
					getActivityInfo();
					mAdapter = new InfoListAdapter(myActivityMap);
					listActivityInfo.setAdapter(mAdapter);
					mAdapter.notifyDataSetChanged();
					break;
				}
			}
		});
		((RadioButton)rg.findViewById(R.id.rbAllActivity)).setChecked(true);
		
		ActivityInfo info1 = new ActivityInfo("2015-1-11", "9：00", "2小时", "关谷",
				"未来科技城", "训练", "张三", "1355555555", "第一次活活动");
		ActivityInfo info2 = new ActivityInfo("2015-1-11", "9：00", "2小时", "吴家山",
				"关南", "训练", "李四", "1355555555", "第一次活活动");
		ActivityInfo info3 = new ActivityInfo("2015-1-11", "9：00", "2小时", "水果湖",
				"东湖", "训练", "王五", "1355555555", "第一次活活动");
		
		allActivityMap.clear();
		allActivityMap.put(0, info1);
		allActivityMap.put(1, info2);
		allActivityMap.put(2, info3);
		
	}
	
	private void getActivityInfo(){
		DataBaseUtil dbUtil = new DataBaseUtil(BycecleActivity.this);
		int size = dbUtil.getAllActivityInfo().size();
		myActivityMap.clear();
		for(int i=0; i<size; i++){
			myActivityMap.put(i, dbUtil.getAllActivityInfo().get(i));
		}
	}
	
	private class InfoListAdapter extends BaseAdapter{
		private SparseArray<ActivityInfo> acitivityMap;
		
		public InfoListAdapter(SparseArray<ActivityInfo> acitivityMap){
			this.acitivityMap = acitivityMap;
		}

		@Override
		public int getCount() {
			return acitivityMap.size();
		}

		@Override
		public Object getItem(int position) {
			return acitivityMap.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = LayoutInflater.from(BycecleActivity.this).inflate(R.layout.acti_bycecle_list_item, null);
			TextView tvStatus = (TextView) convertView.findViewById(R.id.tvStatus);
			tvStatus.setText("未开始");
			
			TextView tvStartDate = (TextView) convertView.findViewById(R.id.tvStartDate);
			tvStartDate.setText(acitivityMap.get(position).startDate);
			
			TextView tvStartTime = (TextView) convertView.findViewById(R.id.tvStartTime);
			tvStartTime.setText(acitivityMap.get(position).startTime);
			
			TextView tvAvtivityTime = (TextView) convertView.findViewById(R.id.tvAvtivityTime);
			tvAvtivityTime.setText(acitivityMap.get(position).activityTime);
			
			TextView tvStartAddress = (TextView) convertView.findViewById(R.id.tvStartAddress);
			tvStartAddress.setText(acitivityMap.get(position).startAddress);
			
			TextView tvEndAddress = (TextView) convertView.findViewById(R.id.tvEndAddress);
			tvEndAddress.setText(acitivityMap.get(position).endAddress);
			return convertView;
		}
		
	}

}
